﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
using Portal.CDSP;

namespace Portal.CDSP
{
    public class FNewsDetailWP : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.FNewsDetailWP";
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string Title
        {
            get
            {
                return GetText("FTitle", "Xem Chi tiết tin tức");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    new ViewCls("default",GetText("default","xem chi tiết tin tức"),1),
                };
            }
        }

        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "FNewsDetailWP";
            }
        }


        private string ClientRenderWebPart(string TabModuleId, string HasPorlet, ref string ModuleTitle)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            try
            {
                string AdvImageId = TabModuleCls.Id;
                string belongSite = TabModuleCls.belongSite;
                string NewsId = (string)Environments.Request("NewsId");
                if (NewsId == null && System.Web.HttpContext.Current.Request.QueryString["NewsId"] == null)
                {
                    return GetText("NewsIdNotFound", "Không tìm thấy NewsId");
                }
                else
                {
                    try
                    {
                        NewsId = System.Web.HttpContext.Current.Request.QueryString["NewsId"];
                    }
                    catch { }
                    NewsDetailCls NewsCls = new NewsDetailBLL().CreateModelIncView(Environments.ActiveLanguage, NewsId);
                    if (NewsCls == null)
                    {
                        return "<div class=cssNewsDeleted>" + GetText("NewsDelete", "Bản tin này đã bị xóa hoặc ngừng phát hành! Xin quay lại sau!") + "</div>";
                    }

                    DataSet ds = new DataSet();
                    int MaxNewsRelation = 5;
                    if (TabModuleCls.XMLConfig != null)
                    {
                        ds.ReadXml(new System.IO.StringReader(TabModuleCls.XMLConfig));
                        MaxNewsRelation = int.Parse((string)ds.Tables[0].Rows[0]["MaxNewsRelation"]);
                    }

                    DataTable dtGroupNewsItemsName = new FSpecialSubjectBLL().ReadingGroupNewsItemName(NewsCls.GroupNewsItemId);
                    string GroupNewsItemName = (string)dtGroupNewsItemsName.Rows[0]["GroupNewsItemName"];

                    string ImageUrl = Environments.HttpMediaRoot + "/media/" + Environments.PortalAlias + "/News/images/" + belongSite + "/" + NewsCls.ImageUrl;
                    string DescImage = string.Empty;
                    DescImage = new FAlbumBLL().GetSubjectAdvImage(AdvImageId);

                    StringBuilder Html = new StringBuilder();
                    string MenuName = "";
                    string menuUrl = "";

                    string Parent_Menu = "";
                    string Parent_MenuUrl = "";

                    FSpecialSubjectCls GroupNewsItemCls = new FSpecialSubjectBLL().CreateModelItemAlias(NewsCls.GroupNewsItemId);
                    DataTable dtGroup = new FSpecialSubjectBLL().ReadingGroupNewsItemName(GroupNewsItemCls.ParentGroupNewsItemId);
                    if (dtGroup.Rows.Count == 0)
                    {
                        DataTable jdtGroup  = new FSpecialSubjectBLL().ReadingMenu(GroupNewsItemCls.GroupNewsId);
                        
                        MenuName = (string)jdtGroup.Rows[0]["Subject"];
                        menuUrl = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, GroupNewsItemCls.GroupNewsItemId, GroupNewsItemCls.GroupNewsItemName, "_runtime");
                        Parent_Menu = "";

                        Parent_MenuUrl = "";
                    }
                    else
                    {
                        //string GroupNewsId = (string)dtGroup.Rows[0]["GroupNewsId"];
                      

                        //DataTable jdtGroup = new FSpecialSubjectBLL().ReadingParentGroupNewsItemName(GroupNewsItemCls.ParentGroupNewsItemId);
                        //if (jdtGroup.Rows.Count == 0)
                        //{
                        //    jdtGroup = new FSpecialSubjectBLL().ReadingMenu(GroupNewsItemCls.GroupNewsId);
                        //}
                        
                        menuUrl = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, GroupNewsItemCls.GroupNewsItemId, GroupNewsItemCls.GroupNewsItemName, "_runtime");

                        Parent_Menu = (string)dtGroup.Rows[0][belongSite + "GroupNewsItemName"];
                        Parent_MenuUrl = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, GroupNewsItemCls.ParentGroupNewsItemId, Parent_Menu, "_runtime");
                    }
                    NewsCls.ShortContent = Ultility.ProcessEmbObject(belongSite, NewsCls.ShortContent, NewsCls.EmbVideo, NewsCls.EmbAudio);

                    Html.Append(

                        "<div class=\"PageTitleBar\">\n" +
                        "				<ul class=\"breadcrumb\">\n" +
                        "					<li class=\"breadcrumblv1\"><a href=\"" + Parent_MenuUrl + "\">" + Parent_Menu + "</a></li>\n" +
                        "					<li class=\"breadcrumblv2\"><a href=\"" + menuUrl + "\">" + GroupNewsItemName + "</a></li>\n" +
                        "				</ul>\n" +
                        "				<div class=\"clr\"></div>\n" +
                        "</div>\n" +

                        "<div class=\"ArticleDetail\">\n" +
                        "				<h3 class=\"ArticleTittle\">" + NewsCls.Subject + "</h3>\n" +
                        "				<p class=\"date\">Ngày " + NewsCls.EntryDate.ToString("dd-MM-yyyy") + "</p>\n" +
                        "				<p class=\"desc\">" + NewsCls.ShortContent + "</p>\n" +
                    //    "				<table class=\"imageTable\">\n" +
                    //    "					<tbody>\n");
                    //if (NewsCls.SmallImage.Length > 0)
                    //{
                    //    Html.Append(
                    //        "						<tr>\n" +
                    //        "							<td><img src=\"" + NewsCls.ImageUrl + "\" /></td>\n" +
                    //        "						</tr>\n" +
                    //        "						<tr>\n" +
                    //        "							<td class=\"image_desc\">" + DescImage + "</td>\n" +
                    //        "						</tr>\n");
                    //}
                    //Html.Append(
                    //"					</tbody>\n" +
                    //"				</table>\n" +
                    "				<p>\n" +
                    "				" + NewsCls.Body + "	\n" +
                    "				</p>\n" +
                    "				\n" +
                    "				<p><span class=\"author\">" + NewsCls.Owner + "</p>\n" +
                    "				\n" +
                    "				\n" +
                    "			</div>\n");

                    DataTable dtNewsAttachments = new NewsDetailBLL().ReadingAttachmentsNews(NewsCls.NewsId);
                    if (dtNewsAttachments.Rows.Count > 0)
                    {
                        Html.Append(
                        "<div class=\"AttachTitle\">" +
                        "<h4>Danh sách tệp đính kèm</h4><br>" +
                        "</div>");
                        string FileUploadUrl = string.Empty;
                        string FileUploadName = string.Empty;
                        for (int i = 0; i < dtNewsAttachments.Rows.Count; i++)
                        {
                            FileUploadName = dtNewsAttachments.Rows[i]["UploadFileName"].ToString();
                            FileUploadUrl = Environments.HttpMediaRoot + "/media/" + Environments.PortalAlias + "/News/Archives/" + belongSite + "/" + FileUploadName;
                            Html.Append(
                            "<div class=\"AttachHolder\">" +
                            "<a href = \"" + FileUploadUrl + "\"><b>" + (i + 1).ToString() + "." + FileUploadName + "</b></a>" +
                            "</div>");
                        }
                    }

                    DataTable dtNewsRelation = new NewsDetailBLL().dtNewsRelation(Environments.ActiveLanguage, NewsCls.GroupNewsItemId, NewsCls.NewsId, MaxNewsRelation, NewsCls.EntryDate);
                    

                    Html.Append(
                            "<div class=\"ArticleList\">\n" +
                            "			<h2>\n" +
                            "				<span>\n" +
                            "					<span>Tin liên quan</span>\n" +
                            "				</span>\n" +
                            "			</h2>\n" +
                            "			<div class=\"clr\"></div>\n" +
                            "			<ul>\n"
                        );
                    for (int iNews = 0; iNews < dtNewsRelation.Rows.Count; iNews++)
                    {
                        string ShortId = (string)dtNewsRelation.Rows[iNews]["ShortId"];

                        string Subject = "";
                        try
                        {
                            Subject = (string)dtNewsRelation.Rows[iNews]["Subject"];
                        }
                        catch { }

                        DateTime EntryDate = DateTime.Now;
                        try
                        {
                            EntryDate = (DateTime)dtNewsRelation.Rows[iNews]["EntryDate"];
                        }
                        catch { }
                        string ShortContent = string.Empty;
                        try
                        {
                            ShortContent = (string)dtNewsRelation.Rows[iNews]["ShortContent"];
                        }
                        catch { }

                        ShortContent = Ultility.ClearHTMLTags(ShortContent);

                        string Url = Environments.GetNewsUrl(TabModuleCls.belongSite, ShortId, Subject, "_runtime");
                        Html.Append("<li><a href=\"" + Url + "\" title =  '" + ShortContent + "'> " + Subject + "</a> (" + EntryDate.ToString("dd/MM/yyyy") + ")</li>");
                    }

                    Html.Append(
                            "</ul>" +
                        "</div>");

                    DataTable dtNewsOther = new NewsDetailBLL().ReadingNewsOther(Environments.ActiveLanguage, NewsCls.GroupNewsItemId, NewsCls.NewsId, MaxNewsRelation, NewsCls.EntryDate);

                    Html.Append(
                        "<div class=\"ArticleList\">\n" +
                        "			<h2>\n" +
                        "				<span>\n" +
                        "					<span>Tin bài khác</span>\n" +
                        "				</span>\n" +
                        "			</h2>\n" +
                        "			<div class=\"clr\"></div>\n" +
                        "			<ul>\n");
                    for (int iNews = 0; iNews < dtNewsOther.Rows.Count; iNews++)
                    {
                        string ShortId = (string)dtNewsOther.Rows[iNews]["ShortId"];

                        string Subject = "";
                        try
                        {
                            Subject = (string)dtNewsOther.Rows[iNews]["Subject"];
                        }
                        catch { }

                        DateTime EntryDate = DateTime.Now;
                        try
                        {
                            EntryDate = (DateTime)dtNewsOther.Rows[iNews]["EntryDate"];
                        }
                        catch { }
                        string ShortContent = string.Empty;
                        try
                        {
                            ShortContent = (string)dtNewsOther.Rows[iNews]["ShortContent"];
                        }
                        catch { }

                        ShortContent = Ultility.ClearHTMLTags(ShortContent);

                        string Url = Environments.GetNewsUrl(TabModuleCls.belongSite, ShortId, Subject, "_runtime");
                        Html.Append("<li><a href=\"" + Url + "\" title =  '" + ShortContent + "'> " + Subject + "</a> (" + EntryDate.ToString("dd/MM/yyyy") + ")</li>");
                    }
                    Html.Append(
                            "</ul>" +
                        "</div>");
                    return Html.ToString();
                }
            }
            catch (Exception ex)
            {
                return "ERROR:" + ex.Message;
            }
        }


        public override RenderInfoCls ClientRenderWebPartWithInfo(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            RenderInfoCls oRenderInfoCls = new RenderInfoCls();
            try
            {
                if (ViewName.Equals("default"))
                {
                    oRenderInfoCls.Content = ClientRenderWebPart(TabModuleId, HasPorlet, ref oRenderInfoCls.ModuleTitle);
                }

                return oRenderInfoCls;// ResourceBLL.GetText(WebPartId, "ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                oRenderInfoCls.ErrorMsg = ex.Message.ToString();
                oRenderInfoCls.Error = true;
                return oRenderInfoCls;
            }
        }


        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            IdField = "Id";
            SubjectField = "Subject";
            DataTable dtResult = new DataTable();
            dtResult.Columns.Add("Id");
            dtResult.Columns.Add("Subject");
            dtResult.Rows.Add(new object[]{
                "3334B4A6-E3DC-46fe-B4DD-FBBD35CB7AB3","Xem chi tiết tin"
            });
            return dtResult;
        }


        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }

    }
}
